<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>实现循环队列</title>
</head>
<body>
<p>模拟击鼓传花案例</p>
<script>
    class Queue {
        constructor() {
            this.data = []
        }

        enqueue(ele) {
            this.data.push(ele)
        }

        dequeue() {
            return this.data.shift()
        }

        size() {
            return this.data.length
        }
    }

    function cyclcQueue(list, num) {
        const queue = new Queue()
        let weedOut = '', winner = '';

        for (let el of list) {
            queue.enqueue(el)
        }

        while (queue.size() > 1) {
            for (let i = 0; i < num; i++) {
                queue.enqueue(queue.dequeue())
            }
            weedOut = queue.dequeue()
            console.log(`${weedOut} has weed out!`)
        }

        winner = queue.dequeue()
        console.log(`winner is ${winner}`)

    }

    const test = ['a', 'b', 'c', 'd', 'e', 'f']
    cyclcQueue(test, 5)

</script>
</body>
</html>